package com.lonwin.inspectionmanagement.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.PointRecordingContract;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.PointHistoryBean;

import java.util.HashMap;
import java.util.Map;


@ActivityScope
public class PointRecordingPresenter extends BasePresenter<PointRecordingContract.Model, PointRecordingContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    ConfigBean configBean;
    @Inject
    public PointRecordingPresenter(PointRecordingContract.Model model, PointRecordingContract.View rootView) {
        super(model, rootView);
         configBean = DataHelper.getDeviceData(mRootView.getActivity(), AppConstants.EXTRA_CONFIG_DATA);

    }

    /**
     *第一次进入的时候调用
     * @param relid
     */
    public void  firstQuery(String relid)
    {
        Map<String,String> maps=new HashMap<>();
        maps.put("reltype","0");
        maps.put("relid",relid);
        mModel.queryPointHistory(DataUtil.getJNUrl(configBean)+"/webmvc/v1/taskhzz/getQuestListByRelid",maps)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<PointHistoryBean>(mErrorHandler) {
                    @Override
                    public void onNext(PointHistoryBean pointHistoryBean) {
                        if (pointHistoryBean==null){
                            mRootView.showMessage("未查询到打点数据");
                            return;
                        }

                             if(!pointHistoryBean.getCode().equals("0"))
                        {
                            mRootView.showMessage(pointHistoryBean.getInfo());
                            return;
                        }
                      mRootView.queryPointHistorySuccess(pointHistoryBean.getData());
                    }
                });
    }
    /**
     *条件选择的时候使用的
     * @param groupid
     */
    public void  queryPoint(String groupid,String tm)
    {
        Map<String,String> maps=new HashMap<>();
        maps.put("groupid",groupid);
        maps.put("tm",tm);
        mModel.queryPointHistory(DataUtil.getJNUrl(configBean)+"/webmvc/v1/taskhzz/getQuestList",maps)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<PointHistoryBean>(mErrorHandler) {
                    @Override
                    public void onNext(PointHistoryBean pointHistoryBean) {
                        if (pointHistoryBean==null){
                            mRootView.showMessage("未查询到打点数据");
                            return;
                        }

                        if(pointHistoryBean.getCode().equals("0"))
                        {
                            mRootView.showMessage(pointHistoryBean.getInfo());
                            return;
                        }
                        mRootView.queryPointHistorySuccess(pointHistoryBean.getData());
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
